# -*- coding: utf-8 -*-
"""
Created on Sat Feb 23 12:56:45 2013

@author: eaponte
"""

# IEVENT

fieldnames = ["time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"]
g = ['UINT32', 'INT16', 'UINT16', 'INT16', 'UINT32', 'UINT32', 'INT16', \
'INT16', 'INT16', 'INT16', 'UINT16', 'INT16', 'INT16', 'INT16', 'INT16', \
'UINT16', 'INT16', 'INT16', 'INT16', 'INT16', 'UINT16', 'INT16', 'INT16', \
'INT16', 'INT16', 'INT16', 'INT16', 'INT16', 'INT16', 'UINT16']

t = '''void ievent2struct( IEVENT el_ievent, mxArray **ievent ){\n'''
for i in range(30):
    t += '''mxArray *t{0};\n{1}_T *ta{0};\n'''.format(i,g[i].lower())

t += '''\nconst char *fields[] = {"time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"};\n\n
*ievent = mxCreateStructMatrix(1,1,30,fields);\n\n'''
for i in range(30):
    t += '''t{0} = mxCreateNumericMatrix(1,1,mx{1}_CLASS,mxREAL);\n'''.format(i,g[i])
    t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,g[i].lower())
    t += '''*ta{0} = ({1}_T ) el_ievent.{2};\n'''.format(i,g[i].lower(),fieldnames[i])
    t += '''mxSetFieldByNumber(*ievent, 0,{0},t{0});\n\n'''.format(i)
t += '''
return;
}'''


# FEVENT


fieldnames = ["time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"]
g = ['UINT32', 'INT16', 'UINT16', 'INT16', 'UINT32', 'UINT32', 'float', 
     'float', 'float', 'float', 'float', 'float', 'float', 'float', 'float', 
     'float', 'float', 'float', 'float', 'float', 'float', 'float', 'float', 
     'float', 'float', 'float', 'float', 'float', 'float', 'UINT16']

t += '''\nvoid fevent2struct( FEVENT el_fevent, mxArray **fevent ){\n'''
for i in range(30):
    if g[i] == 'float':
        t += '''mxArray *t{0};\n{1} *ta{0};\n'''.format(i,g[i].lower())
    else:        
        t += '''mxArray *t{0};\n{1}_T *ta{0};\n'''.format(i,g[i].lower())

t += '''\nconst char *fields[] = {"time","type","read","eye","sttime","entime","hstx","hsty",
"gstx","gsty","sta","henx","heny","genx","geny","ena","havx","havy","gavx",
"gavy","ava", "avel","pvel","svel","evel","supd_x","eupd_x","supd_y",
"eupd_y","status"};\n\n
*fevent = mxCreateStructMatrix(1,1,30,fields);\n\n'''
for i in range(30):
    if g[i] == 'float':
        t += '''t{0} = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);\n'''.format(i,g[i])
        t += '''ta{0} = ({1} *) mxGetData(t{0});\n'''.format(i,g[i].lower())
        t += '''*ta{0} = ({1} ) el_fevent.{2};\n'''.format(i,g[i].lower(),fieldnames[i])
    else:
        t += '''t{0} = mxCreateNumericMatrix(1,1,mx{1}_CLASS,mxREAL);\n'''.format(i,g[i])
        t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,g[i].lower())
        t += '''*ta{0} = ({1}_T ) el_fevent.{2};\n'''.format(i,g[i].lower(),fieldnames[i])
    t += '''mxSetFieldByNumber(*fevent, 0,{0},t{0});\n\n'''.format(i)
t += '''
return;
}'''

# ISAMPLE 
exc = [0,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,8]

types = ['UINT32', 'INT16', 'UINT16', 'INT16', 'INT16', 'INT16', 'INT16', 
         'UINT16', 'INT16', 'INT16', 'INT16', 'INT16', 'UINT16', 'UINT16', 
         'UINT16', 'INT16', 'INT16']
         
fieldnames = ['time', 'type', 'flags', 'px', 'py', 'hx', 'hy', 'pa', 'gx', 
              'gy', 'rx', 'ry', 'status', 'input', 'buttons', 'htype', 
              'hdata']
              
t += '''\nvoid isample2struct( ISAMPLE el_isample, mxArray **isample ){\n'''
t += '''const char *fieldnames[] = {"time","type","flags","px","py","hx","hy",
    "pa","gx","gy","rx","ry","status","input","buttons","htype","hdata"};\n'''

for i in range(len(fieldnames)):
    t += '''mxArray *t{0};\n{1}_T *ta{0};\n'''.format(i,types[i].lower())
t += '''\n*isample = mxCreateStructMatrix(1,1,17,fieldnames);\n\n'''
for i in range(len(fieldnames)):
    if exc[i] == 0:
        t += '''t{0} = mxCreateNumericMatrix(1,1,mx{1}_CLASS,mxREAL);\n'''.format(i,types[i])
        t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
        t += '''*ta{0} = ({1}_T ) el_isample.{2};\n'''.format(i,types[i].lower(),fieldnames[i])
    else:
        t += '''t{0} = mxCreateNumericMatrix(1,{2},mx{1}_CLASS,mxREAL);\n'''.format(i,types[i],exc[i])
        t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
        for j in range(exc[i]):
            t += '''ta{0}[{3}] = ({1}_T ) el_isample.{2}[{3}];\n'''.format(i,
                types[i].lower(),fieldnames[i],j)
            
    t += '''mxSetFieldByNumber(*isample, 0,{0},t{0});\n\n'''.format(i)

t +='''return; \n'''
t+='''}\n'''

# FSAMPLE

exc = [0,0,0,2,2,2,2,2,2,2,0,0,0,0,0,0,8]

types = ['UINT32', 'INT16', 'UINT16', 'float', 'float', 'float', 'float', 
         'float', 'float', 'float', 'float', 'float', 'UINT16', 'UINT16', 
         'UINT16', 'INT16', 'INT16']
         
fieldnames = ['time', 'type', 'flags', 'px', 'py', 'hx', 'hy', 'pa', 'gx', 
              'gy', 'rx', 'ry', 'status', 'input', 'buttons', 'htype', 
              'hdata']
              
t += '''\nvoid fsample2struct( FSAMPLE el_fsample, mxArray **fsample ){\n'''
t += '''const char *fieldnames[] = {"time","type","flags","px","py","hx","hy",
    "pa","gx","gy","rx","ry","status","input","buttons","htype","hdata"};\n'''

for i in range(len(fieldnames)):
    if types[i] == 'float':
        t += '''mxArray *t{0};\n{1} *ta{0};\n'''.format(i,types[i].lower())
    else:
        t += '''mxArray *t{0};\n{1}_T *ta{0};\n'''.format(i,types[i].lower())

t += '''\n*fsample = mxCreateStructMatrix(1,1,17,fieldnames);\n\n'''

for i in range(len(fieldnames)):
    if types[i] == 'float':
        if exc[i] == 0:
            t += '''t{0} = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);\n'''.format(i,types[i])
            t += '''ta{0} = ({1} *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            t += '''*ta{0} = ({1} ) el_fsample.{2};\n'''.format(i,types[i].lower(),fieldnames[i])
        else:
            t += '''t{0} = mxCreateNumericMatrix(1,{2},mxSINGLE_CLASS,mxREAL);\n'''.format(i,types[i],exc[i])
            t += '''ta{0} = ({1} *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            for j in range(exc[i]):
                t += '''ta{0}[{3}] = ({1} ) el_fsample.{2}[{3}];\n'''.format(i,
                    types[i].lower(),fieldnames[i],j)
    else:
        if exc[i] == 0:
            t += '''t{0} = mxCreateNumericMatrix(1,1,mx{1}_CLASS,mxREAL);\n'''.format(i,types[i])
            t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            t += '''*ta{0} = ({1}_T ) el_fsample.{2};\n'''.format(i,types[i].lower(),fieldnames[i])
        else:
            t += '''t{0} = mxCreateNumericMatrix(1,{2},mx{1}_CLASS,mxREAL);\n'''.format(i,types[i],exc[i])
            t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            for j in range(exc[i]):
                t += '''ta{0}[{3}] = ({1}_T ) el_fsample.{2}[{3}];\n'''.format(i,
                    types[i].lower(),fieldnames[i],j)
            
    t += '''mxSetFieldByNumber(*fsample, 0,{0},t{0});\n\n'''.format(i)

t +='''return; \n'''
t+='''}\n'''

# ILINKDATA    

fieldnames = ['time', 'version', 'samrate', 'samdiv', 'prescaler', 
              'vprescaler', 'pprescaler', 'hprescaler', 'sample_data', 
              'event_data', 'event_types', 'in_sample_block', 
              'in_event_block', 'have_left_eye', 'have_right_eye', 
              'last_data_gap_types', 'last_data_buffer_type', 
              'last_data_buffer_size', 'control_read', 'first_in_block',
              'last_data_item_time', 'last_data_item_type', 
              'last_data_item_contents', 'last_data_item', 'block_number',
              'block_sample', 'block_event', 'last_resx', 'last_resy',
              'last_pupil', 'last_status', 'queued_samples', 'queued_events',
              'queue_size', 'queue_free', 'last_rcve_time', 'samples_on', 
              'events_on', 'packet_flags', 'link_flags', 'state_flags',
              'link_dstatus', 'link_pendcmd', 'reserved', 'our_name', 
              'our_address', 'eye_name', 'eye_address', 'ebroadcast_address',
              'rbroadcast_address', 'polling_remotes', 'poll_responses', 
              'nodes']
types = ['UINT32', 'UINT32', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 
         'UINT16', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 'byte', 'byte', 
         'byte', 'byte', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 
         'UINT32', 'UINT16', 'UINT16', 'ALL_DATA', 'UINT32', 'UINT32', 
         'UINT32', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 'UINT16', 
         'UINT16', 'UINT16', 'UINT16', 'UINT32', 'byte', 'byte', 'UINT16',
         'UINT16', 'UINT16', 'byte', 'byte', 'UINT16', 'char', 'ELINKADDR', 
         'char', 'ELINKADDR', 'ELINKADDR', 'ELINKADDR', 'UINT16', 'UINT16', 
         'ELINKNODE']
exc = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40, 0, 40,
       0, 0, 0, 0, 0, 4]

chars = ','.join(['"{0}"'.format(i) for i in fieldnames])
chars = 'const char *fieldnames[] = {' + chars + '};\n'

t += '''\nvoid ilinkdata2struct( ILINKDATA ild, mxArray **fsample ){\n'''
t += chars 

for i in range(len(fieldnames)):
    if types[i] == 'float':
        t += '''mxArray *t{0};\n{1} *ta{0};\n'''.format(i,types[i].lower())
    elif types[i] == 'char':
        pass
    elif types[i] == 'byte':
        pass
    elif types[i] == 'ELINKADDR':
        pass
    elif types[i] == 'ELINKNODE':
        pass
    elif types[i] == 'ALL_DATA':
        pass
    else:
        t += '''mxArray *t{0};\n{1}_T *ta{0};\n'''.format(i,types[i].lower())

for i in range(len(fieldnames)):
    if types[i] == 'float':
        if exc[i] == 0:
            t += '''t{0} = mxCreateNumericMatrix(1,1,mxSINGLE_CLASS,mxREAL);\n'''.format(i,types[i])
            t += '''ta{0} = ({1} *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            t += '''*ta{0} = ({1} ) el_fsample.{2};\n'''.format(i,types[i].lower(),fieldnames[i])
        else:
            t += '''t{0} = mxCreateNumericMatrix(1,{2},mxSINGLE_CLASS,mxREAL);\n'''.format(i,types[i],exc[i])
            t += '''ta{0} = ({1} *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            for j in range(exc[i]):
                t += '''ta{0}[{3}] = ({1} ) el_fsample.{2}[{3}];\n'''.format(i,
                    types[i].lower(),fieldnames[i],j)
    elif types[i] == 'char':
        pass
    elif types[i] == 'byte':
        pass
    elif types[i] == 'ELINKADDR':
        pass
    elif types[i] == 'ELINKNODE':
        pass
    elif types[i] == 'ALL_DATA':
        pass
    else:
        if exc[i] == 0:
            t += '''t{0} = mxCreateNumericMatrix(1,1,mx{1}_CLASS,mxREAL);\n'''.format(i,types[i])
            t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            t += '''*ta{0} = ({1}_T ) el_fsample.{2};\n'''.format(i,types[i].lower(),fieldnames[i])
        else:
            t += '''t{0} = mxCreateNumericMatrix(1,{2},mx{1}_CLASS,mxREAL);\n'''.format(i,types[i],exc[i])
            t += '''ta{0} = ({1}_T *) mxGetData(t{0});\n'''.format(i,types[i].lower())
            for j in range(exc[i]):
                t += '''ta{0}[{3}] = ({1}_T ) el_fsample.{2}[{3}];\n'''.format(i,
                    types[i].lower(),fieldnames[i],j)
            
    t += '''mxSetFieldByNumber(*fsample, 0,{0},t{0});\n\n'''.format(i)

t +='''return; \n'''
t+='''}\n'''

t += '\n*fsample = mxCreateStructMatrix(1,1,{0},fieldnames);\n\n'.format(len(fieldnames))

print(t)


